import org.molamil.acto.core.EventConstants;
import org.molamil.acto.core.Logger;
import org.molamil.acto.pages.master.AbstractExternalMaster;
import org.molamil.acto.pages.master.AbstractMovieMaster;
import org.molamil.acto.pages.master.PageMaster;
import org.molamil.acto.pages.master.PageMasterDelegate;
import org.molamil.acto.props.PropValueResolver;

import mx.utils.Delegate;


/**
 * <code>PageMaster</code> that uses external SWF files as pages.<br />
 * Dispatches:
 * <ul>
 * 	<li><code>onPageLoadStart</code>: The page has been requested.</li>
 * 	<li><code>onPageClear</code>: The page has been cleared.</li>
 * 	<li><code>onMcStateChange</code>: The <code>state</code> of the MovieClip wrapper has changed.</li>
 * </ul>
 *
 * @author Jorge Hernandez
 */
class org.molamil.acto.pages.master.ExternalPageMaster extends AbstractExternalMaster implements PageMaster {
	
	
	/*
	XXX: Refactor masters so Units and Pages have common implementation. Unify events.
		 Let the controller dispatch page and movie named events.
	*/
	
	
	// Properties
	
	private static var log:Logger = Logger.getLogger("ExternalPageMaster");
	
	private var pageMasterDelegate:PageMasterDelegate;	private var isPropsLoadComplete:Boolean;	private var isLoadComplete:Boolean;
	
	
	// Constructor
	
	public function ExternalPageMaster() {
		log.debug("Constructor");
		pageMasterDelegate = new PageMasterDelegate(this);
		isPropsLoadComplete = false;
		isLoadComplete = false;
	}
	
	
	// Functions
	
	public function display():Void {
		
		log.info("display(), name = " + context.name);
		
		super.display();
		
		var propResolver:PropValueResolver = new PropValueResolver();
		propResolver.addEventListener(EventConstants.PROPS_LOAD_COMPLETE, Delegate.create(this, onPropsLoadComplete));
		propResolver.resolveHref(context.props);
		
		var eventObject:Object = {target: this, type: EventConstants.PAGE_DISPLAY};
		dispatchEvent(eventObject);
		
	}
	
	public function clear():Void {
		
		log.info("clear(), name = " + context.name);
		
		super.clear();
		
		var eventObject:Object = {target: this, type: EventConstants.PAGE_CLEAR};
		dispatchEvent(eventObject);
		
	}
	
	private function onPropsLoadComplete(eventObject:Object):Void {
		
		log.info("onPropsLoadComplete(), name = " + context.name);
		
		isPropsLoadComplete = true;
		if (isLoadComplete) {
			doInit();
			dispatchEvent({target: this, type: EventConstants.PAGE_LOAD_INIT, target_mc: loadTarget_mc});
		}
		
	}
	
	private function onLoadStartListener(target_mc:MovieClip):Void {
		
		log.debug("onLoadStartListener(), name = " + context.name);
		
		var eventObject:Object = {target: this, type: EventConstants.PAGE_LOAD_START, target_mc: target_mc};
		dispatchEvent(eventObject);
		
	}
	
	private function onLoadProgressListener(target_mc:MovieClip, bytesLoaded:Number, 
											bytesTotal:Number):Void {
												
		log.debug("onLoadProgressListener(), name = " + context.name);
		log.debug("bytesLoaded = " + bytesLoaded);
		log.debug("bytesLoaded = " + bytesTotal);
		
		var eventObject:Object = {target: this, type: EventConstants.PAGE_LOAD_PROGRESS, target_mc: target_mc, 
								  bytesLoaded: bytesLoaded, bytesTotal: bytesTotal};
		dispatchEvent(eventObject);
		
	}
	
	private function onLoadInitListener(target_mc:MovieClip):Void {
		
		log.info("onLoadInitListener(), name = " + context.name);
		
		super.onLoadInitListener(target_mc);
		
		isLoadComplete = true;
		if (isPropsLoadComplete) {
			doInit();
			var eventObject:Object = {target: this, type: EventConstants.PAGE_LOAD_INIT, target_mc: target_mc};
			dispatchEvent(eventObject);
		}
		
	}
	
	private function onLoadErrorListener(target_mc:MovieClip, errorCode:String, 
											 httpStatus:Number):Void {
											 	
		log.warning("onLoadErrorListener(), name = " + context.name);
		
		super.onLoadErrorListener(target_mc, errorCode, httpStatus);
		
		var eventObject:Object = {target: this, type: EventConstants.PAGE_LOAD_ERROR, target_mc: target_mc, 
								  errorCode: errorCode, httpStatus: httpStatus};
		dispatchEvent(eventObject);
		
	}
	
	private function mcStateChangeListener(eventObject:Object):Void {
		
		super.mcStateChangeListener(eventObject);
		
		log.debug("mcStateChangeListener(), state = " + eventObject.state);
		
		pageMasterDelegate.mcStateChangeListener(eventObject);
		
	}
	
	
}